package com.helloandroid.dialogs

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.helloandroid.R
import com.helloandroid.base.BaseDialog
import java.lang.RuntimeException

class SingleViewHolder(val rootView: View,
                       val tvValue:TextView,
                       val ivCheckable:ImageView
):RecyclerView.ViewHolder(rootView)

class SingleAdapter<T>(private val picker:SinglePicker<T>): RecyclerView.Adapter<SingleViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SingleViewHolder {
        val rootView = LayoutInflater.from(parent.context).inflate(R.layout.item_single_picker,parent,false)
        return SingleViewHolder(
                rootView,
                rootView.findViewById(R.id.tvValue),
                rootView.findViewById(R.id.ivCheckable)
        )
    }

    override fun onBindViewHolder(holder: SingleViewHolder, position: Int) {
        val item = picker.dataSource[position]
        holder.tvValue.text = "$item"
        if(item == picker.value){
            holder.ivCheckable.visibility = View.VISIBLE
        }else{
            holder.ivCheckable.visibility = View.INVISIBLE
        }
        holder.rootView.setOnClickListener {
            if(picker.value != item){
                picker.value = item
                picker.singleAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getItemCount(): Int {
        return picker.dataSource.size
    }
}

fun interface IValueChange<T>{
    fun valueChange(newValue:T)
}

class SinglePicker<T>(override var ctx:Context, var value:T, val dataSource:List<T>) :BaseDialog(ctx){
    lateinit var tvTitle:TextView
    lateinit var tvListView:RecyclerView
    lateinit var singleAdapter:SingleAdapter<T>
    var valueChangeListener:IValueChange<T>? = null

    var title:String = "请选择"

    override fun getContentViewResId() = R.layout.single_picker

    override fun initView() {
        tvTitle = findViewById(R.id.tvTitle)
        tvListView = findViewById(R.id.tvListView)
        tvTitle.text = title

        // setup views
//        val layoutManager = LinearLayoutManager(ctx)
//        layoutManager.orientation = LinearLayoutManager.VERTICAL
//        tvListView.layoutManager = layoutManager

        singleAdapter = SingleAdapter(this)
        tvListView.adapter = singleAdapter

        val  valueIdx = dataSource.indexOf(value)
        if (valueIdx < 0){
            throw RuntimeException("init value not in dataSource!")
        }
        tvListView.scrollToPosition(valueIdx)

        // btns
        findBtn(R.id.btnCancel).setOnClickListener {
            cancel()
        }
        findBtn(R.id.btnOk).setOnClickListener {
            // 更新年龄
            valueChangeListener?.valueChange(value)
            cancel()
        }
    }

    override fun initData() {
    }

    override fun dismiss() {
        valueChangeListener = null
        super.dismiss()
    }

    override fun cancel() {
        valueChangeListener = null
        super.cancel()
    }
}